//===============================================================================
// Microsoft patterns & practices
// Guidance Explorer Offline Client
//===============================================================================
// Copyright  Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;


using GuidanceExplorer.Model;
using GuidanceExplorer.Services;
using GuidanceExplorer.Controls;
using GuidanceExplorerAddIn;
using GuidanceExplorer.Views;

namespace GuidanceExplorerAddIn.Views
{
    public partial class GuidanceItemView : UserControl
    {
        #region Private members

        protected GuidanceItemFileReference _guidanceItem;

        /// <summary>
        /// The main model service instance.
        /// </summary>
        protected IModelStorageService _modelService;

        /// <summary>
        /// The HTML rendering service for the guidance items.
        /// </summary>
        protected IHtmlRenderService _htmlRenderService;

        /// <summary>
        /// The main work item instance.
        /// </summary>
        protected GEController _workItem;

        #endregion

        #region Services properties

        /// <summary>
        /// The HTML rendering service for the guidance items.
        /// </summary>
        public IHtmlRenderService RenderService
        {
            set 
            { 
                _htmlRenderService = value; 
                detailView.RenderService = _htmlRenderService; 
            }
        }

        /// <summary>
        /// The main model service instance.
        /// </summary>
        public IModelStorageService ModelService
        {
            set 
            {
                _modelService = value;
                detailView.ModelService = _modelService;
            }
        }

        /// <summary>
        /// The main work item instance.
        /// </summary>
        public GEController Controller
        {
            set 
            { 
                _workItem = value;
                detailView.Controller = _workItem;
            }
        }

        #endregion

        public GuidanceItemView()
        {
            InitializeComponent();
        }

        //protected override void OnLoad(EventArgs e)
        //{
        //    base.OnLoad(e);
        //    detailView.RenderService = _htmlRenderService;
        //    detailView.ModelService = _modelService;
        //    detailView.Controller = _workItem;
        //}

        private void btnClose_Click(object sender, EventArgs e)
        {
            if (ParentForm != null)
            {
                ParentForm.Close();
            }
        }

         /// <summary>
        /// Show the details of the item in the details pane.
        /// </summary>
        public void ShowDetails(GuidanceItemFileReference guidanceItem)
        {
            _guidanceItem = guidanceItem;
            detailView.ShowDetails(guidanceItem);
        }
        private void btnEdit_Click(object sender, EventArgs e)
        {
            _workItem.GuidanceItemOpened(this, new GuidanceItemFileReferenceEventArgs(new GuidanceItemFileReference[] { _guidanceItem }, ReadProtection.ReadWrite));
            if (ParentForm != null)
            {
                ParentForm.Close();
            }
        }

        private void GuidanceItemView_ParentChanged(object sender, EventArgs e)
        {
            if (ParentForm != null)
            {
                ParentForm.CancelButton = btnClose;
            }
        }
    }
}